/**
 * 处理微信小程序相关API
 */
import $store from '@/store'
import {clone} from 'lodash'

// 获取code
const getWxCode = async () => {
	let code = ''
	const loginResult = await uni.login()
	if (loginResult.errMsg === 'login:ok') {
		code = loginResult.code
	}
	return code
}

// 检查小程序版本更新
const checkUpdate = async () => {
	if (uni.canIUse('getUpdateManager')) {
		const updateManager = uni.getUpdateManager()
		if(!updateManager) return
		updateManager.onCheckForUpdate(function (res) {
			if (res.hasUpdate) {
				updateManager.onUpdateReady(function () {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success: function (res) {
							if (res.confirm) {
								updateManager.applyUpdate()
							}
						}
					})
				})
			}
		})
	}
}


// 获取小程序胶囊信息
const getCapsule = () => {
	let capsule = uni.getMenuButtonBoundingClientRect()
	if (!capsule) {
		capsule = {
			bottom: 56,
			height: 32,
			left: 278,
			right: 365,
			top: 24,
			width: 87
		}
	}
	return capsule
}

export default {
	checkUpdate,
	getWxCode,
	getCapsule
}
